package org.swimv2.controller;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.jboss.ejb3.annotation.RemoteBinding;
import org.swimv2.exception.InvalidLoginException;
import org.swimv2.model.Persona;

/**
 * Session Bean implementation class GestioneLogin
 */
@Stateless
@RemoteBinding(jndiBinding="GestioneLoginRemote")
public class GestioneLogin implements GestioneLoginRemote {
@PersistenceContext(unitName="swimv2db")
private EntityManager manager;

public static final String JNDI_NAME="GestioneLoginRemote";
    /**
     * Default constructor. 
     */

@Override
public Persona validaLogin(String userId, String password)
		throws InvalidLoginException {
	try{
		Query q = manager.createNamedQuery("Persona.findByUsername");
		q.setParameter("username", userId);
		Persona p = (Persona)q.getSingleResult();
		if(p.getPassword().equals(password))
				{
				p.getAbilitas();
				return p;
				}
		else{
			throw new InvalidLoginException();
		}
	}catch (NoResultException e){
		throw new InvalidLoginException();
	}
}
   

}
